<script setup lang="ts">
import {markRaw, onMounted, ref} from "vue";
import type {EChartsOption, EChartsType} from "echarts";
import type {LineBar} from "@/interface";
import * as echarts from "echarts";
import {useGetLineBarDataApi} from "@/api";

const lineBar = ref()
const option = ref<EChartsOption>()
const myChart = ref<EChartsType>()
const LineBarData = ref<LineBar>({
  data: [],
  category: [],
  edges: []
})
const colors = ['#5470C6', '#EE6666'];
const init = async () => {
  const {data: res} = await useGetLineBarDataApi()
  LineBarData.value = res
  myChart.value = markRaw(echarts.init(lineBar.value));
  draw();
}
const draw = () => {
  option.value = {
    color: colors,
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      }
    },
    grid: {
      right: '20%'
    },
    legend: {
      data: ['朝向数量', '平均单价']
    },
    xAxis: [
      {
        type: 'category',
        axisTick: {
          alignWithLabel: true
        },
        // prettier-ignore
        data: LineBarData.value.name
      }
    ],
    yAxis: [
      {
        type: 'value',
        name: '朝向数量',
        position: 'left',
        alignTicks: true,
        axisLine: {
          show: true,
          lineStyle: {
            color: colors[0]
          }
        },
        axisLabel: {
          formatter: '{value} '
        }
      },
      {
        type: 'value',
        name: '平均单价',
        position: 'right',
        alignTicks: true,
        axisLine: {
          show: true,
          lineStyle: {
            color: colors[1]
          }
        },
        axisLabel: {
          formatter: '{value} 元/㎡'
        }
      },
    ],
    series: [
      {
        name: '朝向数量',
        type: 'bar',
        data: LineBarData.value.bar_data
      },
      {
        name: '平均单价',
        type: 'line',
        yAxisIndex: 1,
        data: LineBarData.value.line_data
      }
    ]
  }
  myChart.value.setOption(option.value, {notMerge: true});
}
onMounted(() => {
  init()
})
</script>

<template>
  <div ref="lineBar" class="line_bar">
  </div>
</template>

<style scoped>
.line_bar {
  width: 100%;
  height: 100%;
}
</style>